
package com.gitee.jmash.storage.client;

import com.gitee.jmash.common.grpc.client.GrpcChannel;
import io.grpc.ManagedChannel;
import jakarta.enterprise.inject.Produces;
import jakarta.inject.Singleton;
import jmash.storage.StorageGrpc;

/** storage Client Config . */
public class StorageClientConfig {

  protected static ManagedChannel channel = null;

  public static synchronized ManagedChannel getManagedChannel() {
    if (null != channel && !channel.isShutdown() && !channel.isTerminated()) {
      return channel;
    }
    // k8s环境获取后端服务,本地环境获取测试服务.
    channel = GrpcChannel.getServiceChannel("jmash-storage-service.jmash.svc.cluster.local");
    return channel;
  }

  /** StorageBlockingStub. */
  @Produces
  @Singleton
  public static StorageGrpc.StorageBlockingStub getStorageBlockingStub() {
    return StorageGrpc.newBlockingStub(getManagedChannel());
  }

  /** StorageFutureStub. */
  @Produces
  @Singleton
  public static StorageGrpc.StorageFutureStub getStorageFutureStub() {
    return StorageGrpc.newFutureStub(getManagedChannel());
  }

  /** StorageStub. */
  @Produces
  @Singleton
  public static StorageGrpc.StorageStub getStorageStub() {
    return StorageGrpc.newStub(getManagedChannel());
  }
}
